﻿using JQData.Application.DataFetch;
using JQData.Core.DbModel;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JQData.Application.Apis
{
    public class HotMoneyStockApi(ILogger<HotMoneyStockApi> _log,
        IRepository<HotMoney> _hmRepository,
        IRepository<HotMoneyStockCycleProfit> _hmscpDb,
        HotMoneyStockDataFetch _hotMoneyStockDataFetch) : IDynamicApiController
    {


        [HttpPost]
        public async Task<PagedList<StockHotMoneyData>> GetList(HotMoneyStockGetListRequest request)
        {
            var list = await _hotMoneyStockDataFetch.Load();

            // 过滤
            IEnumerable<StockHotMoneyData> query = list;

            if (!string.IsNullOrWhiteSpace(request.StockCode))
            {
                var code = request.StockCode.Trim();
                query = query.Where(x => !string.IsNullOrEmpty(x.StockCode) && x.StockCode.Contains(code, StringComparison.OrdinalIgnoreCase));
            }

            if (!string.IsNullOrWhiteSpace(request.StockName))
            {
                var name = request.StockName.Trim();
                query = query.Where(x => !string.IsNullOrEmpty(x.StockName) && x.StockName.Contains(name, StringComparison.OrdinalIgnoreCase));
            }

            if (request.HoldState.HasValue)
            {
                var hs = request.HoldState.Value;
                query = query.Where(x => x.HoldState == hs);
            }

            if (request.StartDate.HasValue)
            {
                query = query.Where(x => x.FirstBuyData >= request.StartDate.Value);
            }

            if (request.EndDate.HasValue)
            {
                query = query.Where(x => x.FirstBuyData <= request.EndDate.Value);
            }

            // 分页
            var pageIndex = request.PageIndex <= 0 ? 1 : request.PageIndex;
            var pageSize = request.PageSize <= 0 ? 20 : request.PageSize;
            var totalCount = query.Count();
            var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);

            var items = query
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList();

            var pageList = new PagedList<StockHotMoneyData>
            {
                HasPrevPages = pageIndex > 1,
                HasNextPages = pageIndex < totalPages,
                TotalCount = totalCount,
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalPages = totalPages,
                Items = items
            };

            return pageList;
        }


    }


    public class HotMoneyStockGetListRequest
    {
        public string StockCode { get; set; }

        public string StockName { get; set; }

        /// <summary>
        /// 持仓状态（共识状态）
        ///  多头共识,多数了结,新上榜
        /// </summary>
        public HoldState? HoldState { get; set; }

        /// <summary>
        /// 上榜开始时间
        /// </summary>
        public DateOnly? StartDate { get; set; }

        /// <summary>
        /// 上榜结束时间
        /// </summary>
        public DateOnly? EndDate { get; set; }

        public int PageIndex { get; set; } = 1;

        public int PageSize { get; set; } = 20;
    }



}
